<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.username" placeholder="昵称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <!-- <el-button class="filter-item" style="margin-left: 10px;" type="success" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button> -->
    </div>

    <el-table
      :key="tableKey"
      v-loading="false"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理员" prop="id" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.adminName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理操作日志" prop="id" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.logName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作参数" prop="id" align="center" min-width="180">
        <template slot-scope="{row}">
          <span>{{ substring(row.logArgs) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作Ip" prop="id" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.logIp }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtDatetime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[100, 200, 300, 400]"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="operationLog"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="adminDto" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">

        <el-form-item label="昵称" prop="username">
          <el-input v-model="adminDto.username" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="adminDto.state" clearable placeholder="用户状态" style="width: 150px;" class="filter-item" readonly="readonly">
            <el-option style="width: auto" label="启用" :value="1">启用</el-option>
            <el-option style="width: auto" label="停用" :value="0">停用</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="adminDto.account" />
        </el-form-item>
        <el-form-item v-if="dialogStatus==='create'" label="密码" prop="pwd">
          <el-input v-model="adminDto.pwd" type="password" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel(1)">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { operationLog } from '@/api/sys'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'BackUserindex',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      columns: [
        {
          label: 'Checkbox',
          checkbox: true
        },
        {
          label: '',
          key: 'id',
          expand: true
        },
        {
          label: 'Event',
          key: 'event',
          width: 200,
          align: 'left'
        },
        {
          label: 'Scope',
          key: 'scope'
        }
      ],
      tableData: [],
      total: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 10,
        username: ''
      },
      adminDto: {
        id: undefined,
        username: '',
        state: 0,
        roleId: 1,
        roles: [{ id: 1, roleName: 'super' },
          { id: 2, roleName: 'admin' }]
      },
      dialogFormVisible: false,
      editdialog: false,
      addroledialog: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogPvVisible: false,
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        pwd: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }],
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }],
        account: [{ required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }]
      },
      listLoading: false
    }
  },
  created() {
    this.operationLog(this.listQuery)
  },
  methods: {
    substring(param) {
      if (param) {
        return param.substring(0, 100)
      }
      return ''
    },
    // 获取用户列表
    async operationLog(listQuery) {
      // console.log(this.$store.getters.name)
      const res = await operationLog(this.listQuery)
      // console.log(res)
      this.tableData = res.data.data.records
      const listlength = res.data.data.total
      this.total = listlength
    },
    handleFilter() {
      this.listQuery.current = 1
      this.operationLog(this.listQuery)
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    cancel(type) {
      if (type === 1) {
        this.dialogFormVisible = false
        this.operationLog()
      }
      if (type === 2) {
        this.editdialog = false
      }
      if (type === 3) {
        this.addroledialog = false
        this.operationLog()
      }
    },
    resetTemp() {
      this.adminDto = {
        id: undefined,
        username: '',
        gmtDatetime: '',
        state: 0,
        roleId: 1,
        roles: [{ id: 1, role: 'super' },
          { id: 2, role: 'admin' }]
      }
    },
    handleCreate() {
      this.adminDto = {
        id: undefined,
        username: '',
        state: 0,
        roleId: 1
      }
      this.adminDto.roles = this.roles
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleUpdate(row) {
      this.adminDto = row
      this.adminDto.roles = this.roles
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleEditPassword(row) {
      this.adminDto = row
      this.adminDto.roles = this.roles
      this.adminDto.pwd = ''
      this.editdialog = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleAddrole(row) {
      this.adminDto = row
      this.adminDto.roles = this.roles
      this.addroledialog = true
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
